/**
 * 
 */
package it.polimi.politics.ejb.managers;

import it.polimi.politics.dto.PartitoDTO;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import it.polimi.politics.dto.ReferendumDTO;
import it.polimi.politics.dto.UtentiDTO;
import it.polimi.politics.dto.VotoreferendumDTO;
import it.polimi.politics.ejb.entities.Group;
import it.polimi.politics.ejb.entities.Partito;
import it.polimi.politics.ejb.entities.Referendum;
import it.polimi.politics.ejb.entities.Utenti;
import it.polimi.politics.ejb.entities.Votoreferendum;
import it.polimi.politics.managers.ReferendumManager;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;

import com.sun.xml.internal.ws.util.StringUtils;

@Stateless
public class ReferendumMgrBean implements ReferendumManager {

	@PersistenceContext
	private EntityManager em;
	
		
	@Override
	public ReferendumDTO trovaReferendum(int idReferendum){
		Referendum referendum=em.find(Referendum.class, idReferendum);
		
		return convertToDTO(referendum);
	}
	@Override
	public List<ReferendumDTO> visualizzaProposte(UtentiDTO user){
		
		List<Referendum> refList=new ArrayList<Referendum>();
		List<ReferendumDTO> refListDTO=new ArrayList<ReferendumDTO>();
		//refList=em.createNamedQuery(Referendum.FIND_Proposte,Referendum.class).getResultList();
		
		List<Referendum> refListTutti=new ArrayList<Referendum>();
		refListTutti=em.createNamedQuery(Referendum.FIND_ALL, Referendum.class).getResultList();
		String idutente=user.getUsername();
		
		String queryString = "SELECT DISTINCT i FROM Referendum i INNER JOIN i.utentis1 s WHERE s.username!=:idutente";
		Query query = em.createQuery(queryString);

		query.setParameter("idutente", idutente);
		
		refList=query.getResultList();
		
		for(int i = 0; i < refListTutti.size(); i++)
			
			if((!refList.contains(refListTutti.get(i)))&& (refListTutti.get(i).getStato()==0)){
				refListDTO.add(convertToDTO(refListTutti.get(i)));
			}
		return refListDTO;
		
		
	}
	
	
	
	@Override
	public List<ReferendumDTO> visualizzaApprovati(UtentiDTO user){
		
		

		List<Referendum> refList=new ArrayList<Referendum>();
		List<ReferendumDTO> refListDTO=new ArrayList<ReferendumDTO>();
		//refList=em.createNamedQuery(Referendum.FIND_Proposte,Referendum.class).getResultList();
		
		List<Referendum> refListTutti=new ArrayList<Referendum>();
		refListTutti=em.createNamedQuery(Referendum.FIND_ALL, Referendum.class).getResultList();
		String idutente=user.getUsername();
		
		String queryString = "SELECT DISTINCT i FROM Referendum i INNER JOIN i.utentis2 s WHERE s.username!=:idutente";
		Query query = em.createQuery(queryString);

		query.setParameter("idutente", idutente);
		
		refList=query.getResultList();
		
		for(int i = 0; i < refListTutti.size(); i++)
			
			if((!refList.contains(refListTutti.get(i)))&& (refListTutti.get(i).getStato()==1)){
				refListDTO.add(convertToDTO(refListTutti.get(i)));
			}
		return refListDTO;
	}	
		
	@Override
	public List<ReferendumDTO> visualizzaApprovati(){
		List<Referendum> refList=new ArrayList<Referendum>();
		List<ReferendumDTO> refListDTO=new ArrayList<ReferendumDTO>();
		refList=em.createNamedQuery(Referendum.FIND_Approvati,Referendum.class).getResultList();
		
		for(int i = 0; i < refList.size(); i++)
			refListDTO.add(convertToDTO(refList.get(i)));
			
		return refListDTO;
	
	}
	
	@Override
	public void creaProposta(ReferendumDTO referendum){
	Referendum newRef=new Referendum(referendum);
	em.persist(newRef);
	
	}
	
	
	@Override
	public void likeRef(ReferendumDTO referendum, UtentiDTO user){
	Utenti utente=new Utenti(user);
	
	List<Group> groups = new ArrayList<Group>();
	groups.add(Group.USER);
	utente.setGroups(groups);
	
	Referendum refer = new Referendum(referendum);
	List<Referendum> listaref=new ArrayList<Referendum>();
	listaref.add(refer);
	utente.setReferendums2(listaref);
	em.merge(utente);
	}
	/*
	@Override
	public void likeRef(ReferendumDTO referendum){
	String utente=context.getCallerPrincipal().getName();
	Utenti user=em.find(Utenti.class, utente);
	Referendum refer = new Referendum(referendum);
	List<Referendum> listaref=new ArrayList<Referendum>();
	listaref.add(refer);
	user.setReferendums2(listaref);
	em.merge(utente);
	}
	*/
	
	
	@Override
	public void vota(ReferendumDTO referendumDTO, VotoreferendumDTO votoDTO, UtentiDTO user){
		
		System.out.println("a dio non piacevano le patate");
		Utenti utente=new Utenti(user);
		Referendum refer = new Referendum(referendumDTO);
		
		List<Referendum> listaref=new ArrayList<Referendum>();
		listaref.add(refer);
		utente.setReferendums1(listaref);
		
		List<Group> groups = new ArrayList<Group>();
		groups.add(Group.USER);
		utente.setGroups(groups);
		
		
		em.merge(utente);
		
		
		//creazione Voto
		
		Votoreferendum voto=new Votoreferendum(votoDTO); 
		voto.setReferendum(refer);
		em.persist(voto);
		
		
			
	}	
	
	

				
		//HELPER
		
		private ReferendumDTO convertToDTO(Referendum referendum) {
			ReferendumDTO refDTO= new ReferendumDTO();
			refDTO.setIdReferendum(referendum.getIdreferendum());
			refDTO.setStato(referendum.getStato());
			refDTO.setTestoProposta(referendum.getPropostareferendum());
			refDTO.setTitolo(referendum.getTitolo());
			return refDTO;
		}
		
}
